﻿using Microsoft.AspNetCore.Mvc;
using SIMS.Shared.DTO;
using SIMS.WebApi.Models;
using SIMS.WebApi.Services.Menus;

namespace SIMS.WebApi.Controllers
{
    /// <summary>
    /// 菜单控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MenuController: ControllerBase
    {
        private readonly ILogger<MenuController> logger;

        private readonly IMenuAppService _service;

        public MenuController(ILogger<MenuController> logger, IMenuAppService service)
        {
            this.logger = logger;
            this._service = service;
        }

        /// <summary>
        /// 新增Menu
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMenu(MenuDTO menu)
        {
            var data = _service.AddMenu(menu);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 删除Menu
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteMenu(int id)
        {
            var data = _service.DeleteMenu(id);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 获取单个menu
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenu(int id)
        {
            var data = _service.GetMenu(id);
            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="menuName"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenus(string? menuName, int pageNum, int pageSize)
        {
            var data = _service.GetMenus(menuName, pageNum, pageSize);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 修改Menu
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateMenu(MenuDTO menu)
        {
            var data = _service.UpdateMenu(menu);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }
    }
}
